package net.controller;

import net.common.Respose;
import net.common.ServiceResponse;
import net.pojo.Notice;
import net.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/1 9:10
 */
@Controller
@RequestMapping("/notices")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Notice> list = noticeService.findAll();
        model.addAttribute("list",list);
        return "/admin/notices/list.jsp";
    }

    @RequestMapping("/findById")
    public String findById(int n_id,Model model){
        Notice notice = noticeService.findById(n_id);
        model.addAttribute("notice",notice);
        return "/admin/notices/edit.jsp";
    }

//    @RequestMapping("/editNotice")
//    @ResponseBody
//    public Respose editNotice(@RequestBody Notice notice, Model model){
//        int num = noticeService.updateById(notice);
//
//        if (num > 0 ){
//            return new Respose(0,"修改成功");
//        }
//        return new Respose(1,"修改失败");
//    }

    @RequestMapping("/editNew")
    @ResponseBody
    public ServiceResponse editNew(@RequestBody Notice notice){
        ServiceResponse serviceResponse = noticeService.editNotice(notice);
        return serviceResponse;
    }

    @RequestMapping("/deleteById")
    public String deleteById(int n_id,Model model){
        int notice = noticeService.deleteById(n_id);
        return"redirect:/notices/findAll.do";
    }

    @RequestMapping("/addNotice")
    public String addNotice(Notice notice,Model model){
        notice.setN_time(new Date());
        int addNotice = noticeService.addNotice(notice);
        return"redirect:/notices/findAll.do";
    }


}
